package com.huluobo.a2204kotlinshopping.mine

import androidx.lifecycle.MutableLiveData
import com.huluobo.base.bean.CommonBeanResult
import com.huluobo.base.bean.OrderTypeBean
import com.huluobo.base.bean.UserBean
import com.huluobo.base.model.BaseRepo
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.lang.Exception

/**
 *  Created by LC on 2024/8/3.
 */
class MineRepo : BaseRepo() {
    fun queryUserData(username: String, success: MutableLiveData<UserBean>, failed: MutableLiveData<String>) {
        db.userDao.queryUserInfo(username)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<UserBean> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: UserBean) {
                    success.value = t
                }
            })
    }

    fun getAllOrders(success: MutableLiveData<List<OrderTypeBean>>, failed: MutableLiveData<String>) {
        try {
            success.value = db.orderTypeDao.queryOrderTypeBeans()
        } catch (e: Exception) {
            failed.value = e.message
        }
    }

    fun updateAddress(address: String, success: MutableLiveData<CommonBeanResult>, failed: MutableLiveData<String>) {
        apiService.updateAddress(address)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<CommonBeanResult> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onError(e: Throwable) {
                    failed.value = e.message
                }

                override fun onComplete() {
                }

                override fun onNext(t: CommonBeanResult) {
                    success.value = t
                }
            })
    }
}